

let res = "";
let dd = ""
let count = 0
let data_sync = async () => {
  let data = await ajax({
    type: "GET",
    url: "http://127.0.0.1:4000/testPaper/GetTestPaperFind",
    data: undefined,
  });
  

  res = JSON.parse(data);
  
  let url = window.location.search
  let u_id = url.slice(url.lastIndexOf("&")+1)
  
  if (res.code == 200) {
  
 let data = res.obj.filter(v=>{
  return  v._id == u_id
 })
  
 dd= data[0]
 console.log(data[0])
    var swiper = new Swiper({
      obj: ".swiper_content",
      arr: data[0],     //改
      width: 786,
      height: 400,
      autoPlay: false,
      delay: 2000,
      submit: ".btn_test",
    });
    swiper.xuan();

    $(".di").html("第1题");
    $(".gong").html(`第${data[0].exercisesId.length}题`);  //改
    table_xuan(dd.exercisesId); //渲染选项卡   //改


     $('.test_title').html(data[0].title)   //改


  }


$('.p_title_test').find('span')[0].innerHTML= "还剩30分00秒"
dsj()
 function dsj(){
  
                
  let time=1800;//30分钟换算成1800秒
   
  
  setInterval(function(){
      time=time-1;
     var minute=parseInt(time/60);
     var second=parseInt(time%60);
     $('.p_title_test').find('span')[0].innerHTML='还剩'+minute+'分'+second+'秒';
  
  
  },1000)

 }

};

data_sync();

//渲染选项卡

//点击交卷
$(".jiao_btn_juan").on("click", function () {
  if ($(".item_D").length == $(".zhong").length) {
    //  提交卷子
  let testiD =  window.location.search.slice(window.location.search.lastIndexOf("&")+1) 
  let stu =  window.location.search.slice(window.location.search.lastIndexOf("=")+1,window.location.search.indexOf("&")) 
  let obj = {}
  let fin_cout =12- (count *3)
  obj['testId'] = testiD
  obj['studentId'] = stu
  obj['score'] = fin_cout
  console.log(obj)
  ajax({
   type: "POST",
   url:'http://127.0.0.1:4000/tested/CreatTested',
   data:obj,
  })
  let stunum = window.location.search.slice(window.location.search.indexOf("=")+1,window.location.search.indexOf("&"))
  let test = window.location.search.slice(window.location.search.lastIndexOf("&")+1)
   window.location.href=`grade.html?${stunum}&${test}`

  } else {
 
    alert("您还没有答完卷子，不能提交");
  }
});

$(".ti_content").on("click", function (e) {
  if (e.target.className == "en_no") {
    e.target.className = "en_check";

    $(e.target).parents().siblings().find(".en_check").attr("class", "en_no");
  } else if (e.target.className == "en_check") {
    e.target.className = "en_no";
  }
});

//点击下一题
let arr = [];
let arr2 = [];
$(".btn_test").on("click", function (e) {
  let obj = {};
  if (
    $(".ti_content").find(".en_check").parent().parent().parent().length > 0
  ) {
    let id = $(".ti_content")
      .find(".en_check")
      .parent()
      .parent()
      .parent()
      .attr("item_id");
    let ans = $(".ti_content").find(".en_check").attr("sid");
    obj["id"] = id;
    obj["ans"] = ans;
    arr.push(obj);
  }
  let stu = window.location.search.slice(window.location.search.indexOf("=")+1,window.location.search.indexOf("&"))
  let url = window.location.search
  let u_id = url.slice(url.lastIndexOf("&")+1)
  //把写好的题存到local  //学生名字  做的题目
  localStorage.setItem(`${stu}${u_id}`,JSON.stringify(arr))

  
  //点击下一题 获取错题
  let c_t = ""
  let arr3 = [];
  arr.forEach((e) => {
    arr2 = dd.exercisesId.filter((v) => {   //改
   
      if (e.id == v._id && v.answer !== parseInt(e.ans)) {
        c_t = e.ans
        return v;
      }
    });
   
  });
  arr3.push(arr2)
   
  //发起ajax
  if(arr3[0].length>=1){
    count++
    let stu = window.location.search.slice(window.location.search.indexOf("=")+1,window.location.search.indexOf("&"))
 
    arr3[0].forEach(v=>{
     
      let obj_wrong = {}
      obj_wrong['errorAnswer'] = c_t 
      obj_wrong['exerciseId'] = v._id
      obj_wrong['studentId'] = stu
      console.log(obj_wrong)
      
      ajax({
        type: "post",
        url: "http://127.0.0.1:4000/testPaper/CreatTestPaperErr",
        data: obj_wrong,
       })
    })
  
  }
  

 
 
});



function table_xuan(arr) {
  let str = "";
  arr.forEach((v, i) => {
    var item = `
   <div class="item_D fl_left">
   ${i + 1}
 </div>
   `;
    str += item;
  });
  document.querySelector(".table_d_4").innerHTML = str;
}

